package com.test.mooan.labama.dialog_boom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.annotation.NonNull;

import com.test.mooan.labama.R;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;

public class StatementDialog_boom extends Dialog {

    private final String pageTag;

    private final Runnable onAgreeRunnable;

    public StatementDialog_boom(@NonNull Context context,String tag, Runnable onAgreeRunnable) {
        super(context);
        this.pageTag = "DIALOG_STATEMENT_"+tag;
        this.onAgreeRunnable = onAgreeRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_statement_boom);
        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        CheckBox readCb = findViewById(R.id.dialog_statement_cb_read);
        Button denyBtn = findViewById(R.id.dialog_statement_btn_deny);
        Button agreeBtn = findViewById(R.id.dialog_statement_btn_agree);

        denyBtn.setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_DENY_CLICK");
            dismiss();
        });

        agreeBtn.setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_AGREE_CLICK");
            if (onAgreeRunnable==null) {
                dismiss();
                return;
            }
            if (readCb.isChecked()) {
                dismiss();
                onAgreeRunnable.run();
            }
            else {
                new PermissionAgreeDialog_boom(getContext(),null,() -> {
                    readCb.setChecked(true);
                    dismiss();
                    onAgreeRunnable.run();
                }).show();
            }
        });


    }

    @Override
    public void show() {
        super.show();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_END");
    }

}
